package com.example.springdemo.Controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springdemo.entity.Cart;
import com.example.springdemo.entity.Goods;
import com.example.springdemo.service.ICartService;
import com.example.springdemo.utils.PageResult;
import com.example.springdemo.utils.PassToken;
import com.example.springdemo.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * cartController
 * 
 * @author ruoyi
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/system/cart")
public class CartController
{
    @Autowired
    private ICartService cartService;

    /**
     * 查询cart列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Cart cart)
    {
        // startPage();

        return ResponseResult.createBySuccess(200,cartService.selectCartList(cart),"查询成功");
    }
    @PassToken
    @GetMapping("/frontlist")
    public ResponseResult list1(Cart cart) {
        System.out.println(cart.getCartid());
        List<Cart> list = cartService.selectCartListNoPage(cart);
        return ResponseResult.createBySuccess(200,list,"获取成功");
    }

//    /**
//     * 导出cart列表
//     */
//    @Log(title = "cart", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Cart cart)
//    {
//        List<Cart> list = cartService.selectCartList(cart);
//        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
//        util.exportExcel(response, list, "cart数据");
//    }

    /**
     * 获取cart详细信息
     */
    @PassToken
    @GetMapping(value = "/{cartid}")
    public ResponseResult getInfo(@PathVariable("cartid") Long cartid)
    {
        Cart cart = cartService.selectCartByCartid(cartid);
        return ResponseResult.createBySuccess(200,cart,"查询成功");
    }

    /**
     * 新增cart
     */
    @PassToken
    @PostMapping
    public ResponseResult add(@RequestBody Cart cart)
    {
         Cart c1 = new Cart();
         BeanUtils.copyProperties(cart,c1);

        Cart c = cart;
        c.setPrice(null);
        c.setQuantity(null);
        List<Cart> list = cartService.selectCartList(c);

        if(list.size()!=0){
            cart.setUpdateDate(new Date());
            int i = cartService.updateCartNum(c1);  //修改数量
            if(i>0){
                return ResponseResult.createBySuccess(200,"","修改成功");

            }else{
                return ResponseResult.createByFail(500,"修改失败");
            }
        }else{
//            System.out.println(c1.getPrice());
            int i = cartService.insertCart(c1);
            if (i > 0) {
                return ResponseResult.createBySuccess(200, "", "添加成功");
            } else {
                return ResponseResult.createByFail(200, "添加失败");
            }
        }
    }

    /**
     * 修改cart
     */
    @PassToken
    @PutMapping
    public ResponseResult edit(@RequestBody Cart cart)
    {
        int i = cartService.updateCart(cart);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "修改成功");
        } else {
            return ResponseResult.createByFail(200, "修改失败");
        }
    }

    /**
     * 删除cart
     */
    @PassToken
	@DeleteMapping("/{cartids}")
    public ResponseResult remove(@PathVariable Long[] cartids)
    {
        System.out.println(cartids[0]);
        int i = cartService.deleteCartByCartids(cartids);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "删除成功");
        } else {
            return ResponseResult.createByFail(200, "删除失败");
        }
    }
}
